package com.zzj.service.impl;

import cn.hutool.core.lang.id.NanoId;
import com.zzj.entity.Invitation;
import com.zzj.mapper.InvitationMapper;
import com.zzj.model.dto.InvitationCreateDTO;
import com.zzj.service.IInvitationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.utils.Utils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-05-12
 */
@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements IInvitationService {

    @Override
    public Invitation create(InvitationCreateDTO in) {
        String code = NanoId.randomNanoId(16);
        String start = Utils.getNowString();
        String end = Utils.getNowAddString(in.getDuration());

        Invitation invitation = new Invitation();
        invitation.setCode(code);
        invitation.setType(in.getType());
        invitation.setDuration(in.getDuration());
        invitation.setStart(start);
        invitation.setEnd(end);
        boolean save = this.save(invitation);
        return invitation;
    }

    @Override
    public boolean expire(String code) {
        return this.lambdaUpdate().eq(Invitation::getCode, code)
                .set(Invitation::getStatus, 2).update();
    }

    @Override
    public boolean use(String code) {
        return this.lambdaUpdate().eq(Invitation::getCode, code)
                .set(Invitation::getStatus, 1).update();
    }
}
